﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.IO;

public partial class Upload : System.Web.UI.Page
{
    string connectionString;
    private SqlConnection connect;
    private SqlDataAdapter da;
    private SqlCommand command;
    private DataSet ds = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["myCon"].ToString();
        connect = new SqlConnection(connectionString);
        connect.Open();
    }
    protected void getUploadImage(string userr, string imgg)
    {
        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        connect.Open();
        command = new SqlCommand("spInsertImage", connect);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@UserName", SqlDbType.VarChar).Value = userr;
        command.Parameters.Add("@ImageUrl", SqlDbType.VarChar).Value = imgg;
        command.ExecuteNonQuery();
        connect.Close();

    }
    protected void UploadImage(string user)
    {
        string filename = "";
        bool check = false;
        string img;
        string id1, name1, img1;
        float price1;



        if (FileUpload1.HasFile)
        {
            string[] validate = { "jpg", "jpeg"};
            string ext = Path.GetExtension(FileUpload1.PostedFile.FileName);
            for (int i = 0; i < validate.Length; i++)
            {
                if (ext == "." + validate[i])
                {
                    if (FileUpload1.PostedFile.ContentLength <= 5120000)
                    {
                        check = true;

                        break;
                    }
                }
            }
            if (check)
            {
                string imgdir = Server.MapPath("IMG");
                System.IO.Directory.CreateDirectory(imgdir + "\\" + user);
                filename = Path.GetFileName(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/IMG/" + user + "/") + filename);
                Label2.Visible = true;

                //.ImageUrl = "~/IMG/" + filename;
                //Image1.Visible = true;
            }
            else
            {
                Label2.Text = "File must be Images and less than 500kb";
                Label2.Visible = true;
            }
        }

        if (check == true)
        {

            Label2.Text = "Insert Successfully";
            image.ImageUrl = "~/IMG/" + user + "/"+filename;
            getUploadImage(user, "~/IMG/" + user + "/" + filename);
        }
        else if (check == false)
        {
            Label2.Text = "please choose Image";
            Label3.Text = "File must be Images and less than 500kb";
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        UploadImage(Session["User"].ToString());
        
        
    }

    private string _contents;

    public string contents
    {
        get { return _contents; }
        set { _contents = value; }
    }
}
